package net.lab1024.sa.admin.module.business.erp.poo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.bas.dao.BatchordDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.BatchordEntity;
import net.lab1024.sa.admin.module.business.erp.inv.domain.vo.TargetVo;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.SubordDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.SubordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PoordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.SubordAddForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.SubordQueryForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.SubordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.StoordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.SubordEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.SubordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.SubordVO;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.SubordaVO;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 暂收送检单 Service
 *
 * @Author AKITA
 * @Date 2025-07-24 14:41:19
 * @Copyright AKITA
 */

@Service
public class SubordService {

    @Resource
    private SubordDao subordDao;
    @Resource
    private SubordaDao subordaDao;
    @Resource
    private BatchordDao batchordDao;
    @Resource
    private StoordaDao stoordaDao;

    public PageResult<SubordVO> queryPage(SubordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<SubordVO> list = subordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<Long> add(SubordAddForm addForm) {
        SubordEntity subordEntity = SmartBeanUtil.copy(addForm, SubordEntity.class);
        DateTimeFormatter yyyy = DateTimeFormatter.ofPattern("yyyy");
        DateTimeFormatter mm = DateTimeFormatter.ofPattern("MM");
        DateTimeFormatter dd = DateTimeFormatter.ofPattern("dd");
        String year = yyyy.format(LocalDate.now());
        String month = mm.format(LocalDate.now());
        String day = dd.format(LocalDate.now());
        subordDao.insert(subordEntity);
        for (SubordaEntity subordaEntity : addForm.getLista()) {
            BatchordEntity batchordEntity = new BatchordEntity();
            subordaEntity.setId(subordEntity.getId());
            Long batchNum =  batchordDao.selectMaxId(year,month,day);
            String l = (year + month + day + (batchNum));
            subordaEntity.setBatchId(l);
            batchordEntity.setBatchId(l);
            batchordEntity.setBatchNum(batchNum);
            batchordEntity.setYear(year);
            batchordEntity.setMonth(month);
            batchordEntity.setDay(day);
            batchordDao.insert(batchordEntity);
            subordaDao.insert(subordaEntity);
        }







        return ResponseDTO.ok(subordEntity.getId());
    }

    @Transactional
    public ResponseDTO<String> update(SubordUpdateForm updateForm) {

        SubordEntity subordEntity = SmartBeanUtil.copy(updateForm, SubordEntity.class);

        if (subordDao.updateById(subordEntity) == 0){
            throw new RuntimeException("数据已被修改，刷新后重试！");
        }

        List<SubordaVO> lista = updateForm.getLista();
        subordaDao.deleteById(subordEntity.getId());

        for (SubordaEntity subordaEntity : SmartBeanUtil.copyList(lista, SubordaEntity.class)) {
            subordaDao.insert(subordaEntity);
        }


        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        subordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long id) {

        List<StoordaEntity> entities = stoordaDao.selectList(new LambdaQueryWrapper<StoordaEntity>().eq(StoordaEntity::getBaseId, id));
        if (!entities.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在采购入库单，禁止删除！");
        }

        subordDao.deleteById(id);
        subordaDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<SubordVO> info(Long id) {

        SubordEntity subordEntity = subordDao.selectById(id);

        SubordVO copy = SmartBeanUtil.copy(subordEntity, SubordVO.class);
        List<SubordaEntity> subordaEntities = subordaDao.selectList(new LambdaQueryWrapper<SubordaEntity>().eq(SubordaEntity::getId, id));
        List<SubordaVO> subordaVOS = SmartBeanUtil.copyList(subordaEntities, SubordaVO.class);
        ArrayList<TargetVo> targetVob = new ArrayList<>();
        for (SubordaEntity item : subordaEntities) {
            if (item.getBaseId() != null ) {
                TargetVo targetVo = new TargetVo();
                targetVo.setId(item.getBaseId());
                targetVo.setLineNum(item.getBaseLineNum());
                targetVo.setItemId(item.getItemId());
                targetVo.setItemName(item.getItemName());
                targetVo.setQty(item.getQty());
                targetVob.add(targetVo);
            }
        }

        List<StoordaEntity> stoordaEntities = stoordaDao.selectList(new LambdaQueryWrapper<StoordaEntity>().eq(StoordaEntity::getBaseId, id));
        ArrayList<TargetVo> targetVoc = new ArrayList<>();
        for (StoordaEntity item : stoordaEntities) {
            TargetVo targetVo = new TargetVo();
            targetVo.setId(item.getId());
            targetVo.setLineNum(item.getLineNum());
            targetVo.setItemId(item.getItemId());
            targetVo.setItemName(item.getItemName());
            targetVo.setQty(item.getQty());
            targetVoc.add(targetVo);
        }
        copy.setLista(subordaVOS);
        copy.setListb(targetVob);
        copy.setListc(targetVoc);
        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<SubordVO> infoUnexecute(Long id) {
        SubordEntity subordEntity = subordDao.selectById(id);
        SubordVO copy = SmartBeanUtil.copy(subordEntity, SubordVO.class);
        List<SubordaVO> puordaVOList = subordaDao.infoUnexecute(id);
        copy.setLista(puordaVOList);
        return ResponseDTO.ok(copy);


    }
}
